package com.wc.算法基础课.C第三讲搜索与图论.Flood_Fill.扫雷;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/19 13:12
 * @description https://www.acwing.com/problem/content/689/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 310;
    static char[][] g = new char[N][N];
    // 标记雷数
    static int[][] num = new int[N][N];
    static boolean[][] st = new boolean[N][N];
    static int[] dx = new int[]{-1, -1, -1, 0, 0, 1, 1, 1};
    static int[] dy = new int[]{-1, 0, 1, -1, 1, -1, 0, 1};
    static int t, n;

    public static void main(String[] args) {
        t = sc.nextInt();
        for (int m = 1; m <= t; m++) {
            n = sc.nextInt();
            for (int i = 1; i <= n; i++) {
                g[i] = (" " + sc.next()).toCharArray();
            }

            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    num[i][j] = 0;
                    if (g[i][j] == '*') {
                        num[i][j] = -1;
                        st[i][j] = true;
                    } else {
                        for (int k = 0; k < 8; k++) {
                            int x = i + dx[k];
                            int y = j + dy[k];
                            if (isNotBorder(x, y) && g[x][y] == '*') {
                                num[i][j]++;
                            }
                        }
                    }
                }
            }
            int res = 0;
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    if (num[i][j] == 0 && !st[i][j]) {
                        res++;
                        dfs(i, j);
                    }
                }
            }
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    if (!st[i][j]) res++;
                    st[i][j] = false;
                }
            }
            out.printf("Case #%d: %d\n", m, res);
        }
        out.flush();
    }

    static void dfs(int x, int y) {
        st[x][y] = true;

        for (int i = 0; i < 8; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (isNotBorder(nx, ny) && !st[nx][ny]) {
                if (num[nx][ny] > 0) st[nx][ny] = true;
                if (num[nx][ny] == 0) dfs(nx, ny);
            }
        }
    }


    static boolean isNotBorder(int x, int y) {
        return x >= 1 && x <= n && y >= 1 && y <= n;
    }

}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
